package com.zjh.shop.config;

import com.zjh.common.util.UserContext;
import com.zjh.shop.fallback.UserClientFallBackFactory;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

import java.util.Optional;

public class FeignLoggerConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                requestTemplate.header("user-info", Optional.ofNullable(userId).map(v->v.toString()).orElse(null));
            }
        };
    }

    @Bean
    public UserClientFallBackFactory userClientFallBackFactory(){
        return new UserClientFallBackFactory();
    }
}
